const fs = require('fs')
const path = require('path')
const connection = require('../app/database')
const { AVATAR_PATH, WALL_PATH } = require('../app/file-path')

class FileService {

  // 判断用户是否更新头像
  async hasUserAvatar(userId) {
    try {
      const statement = `select filename from avatar where userId = ?`
      const result = await connection.execute(statement, [userId])
      return result[0][0]
    } catch (error) {
      console.log(error)
    }
  }

  // 保存头像信息
  async saveAvatarInfo(filename, mimetype, size, userId) {
    try {
      const res = await this.hasUserAvatar(userId) || {}
      let statement = null
      if (res.filename) {
        // 有头像
        statement = `update avatar set filename = ?, mimetype = ?, size = ? where userId = ?`
      } else {
        // 无头像
        statement = `insert into avatar (filename, mimetype, size, userId) values (?, ?, ?, ?)`
      }
      const result = await connection.execute(statement, [filename, mimetype, size, userId])
      res.filename && await fs.unlinkSync(path.resolve(__dirname, `../../${AVATAR_PATH}/${res.filename}`))
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 获取用户头像
  async getAvatarByUserId(userId) {
    try {
      const statement = `select * from avatar where userId = ?`
      const [result] = await connection.execute(statement, [userId])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 判断用户是否更新壁纸
  async hasUserWall(userId) {
    try {
      const statement = `select filename from wall where userId = ?`
      const result = await connection.execute(statement, [userId])
      return result[0][0]
    } catch (error) {
      console.log(error)
    }
  }

  // 保存壁纸信息
  async saveWallInfo(filename, mimetype, size, userId) {
    try {
      const res = await this.hasUserWall(userId) || {}
      let statement = null
      if (res.filename) {
        // 有壁纸
        statement = `update wall set filename = ?, mimetype = ?, size = ? where userId = ?`
      } else {
        // 无壁纸
        statement = `insert into wall (filename, mimetype, size, userId) values (?, ?, ?, ?)`
      }
      const result = await connection.execute(statement, [filename, mimetype, size, userId])
      res.filename && await fs.unlinkSync(path.resolve(__dirname, `../../${WALL_PATH}/${res.filename}`))
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 获取用户壁纸
  async getWallByUserId(userId) {
    try {
      const statement = `select * from wall where userId = ?`
      const [result] = await connection.execute(statement, [userId])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 保存动态配图
  async savePicInfo(mimetype, userId, momentId, filename, url) {
    try {
      const statement = `insert into moment_pic (mimetype, userId, momentId, filename, url) values (?, ?, ?, ?, ?)`
      const result = await connection.execute(statement, [mimetype, userId, momentId, filename, url])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 获取动态配图
  async getPicByMomentId(momentId, filename) {
    try {
      const statement = `select mimetype from moment_pic where momentId = ? and filename = ?`
      const result = await connection.execute(statement, [momentId, filename])
      return result[0][0]
    } catch (error) {
      console.log(error)
    }
  }

}

module.exports = new FileService()
